package br.com.jbssistemas.loterica.managedbeans;

import br.com.jbssistemas.loterica.daos.DAOGenerico;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ActionEvent;

/**
 *
 * @author daniel
 * @param <T>
 *
 */

public class AbstractBean<T> {

    private DAOGenerico<T> daoGenerico;
    private Class<T> itemClass;
    private T selected;
    private Collection<T> items;

    private enum PersistAction {

        CREATE,
        DELETE,
        UPDATE

    }

    public AbstractBean() {
        
    }

    public AbstractBean(Class<T> itemClass, DAOGenerico<T> daoGenerico) {

        this.daoGenerico = daoGenerico;
        
        this.itemClass = itemClass;

        try {
            
            selected = itemClass.newInstance();
            
        } catch (InstantiationException ex) {
            
            Logger.getLogger(AbstractBean.class.getName()).log(Level.SEVERE, null, ex);
            
        } catch (IllegalAccessException ex) {
            
            Logger.getLogger(AbstractBean.class.getName()).log(Level.SEVERE, null, ex);
            
        }

    }

    public Class<T> getItemClass() {
        
        return itemClass;
        
    }
    
    public void setItemClass(Class<T> itemClass) {
        
        this.itemClass = itemClass;
        
    }

    public T getSelected() {
        
        return selected;
        
    }

    public void setSelected(T selected) {
        
        this.selected = selected;
        
    }

    public Collection<T> getItems() {

        if (items == null) {

            items = this.daoGenerico.findAll();

        }

        return items;
    }

    public void setItems(Collection<T> items) {
        
        this.items = items;
        
    }

    public T prepareCreate(ActionEvent event) {
        
        T newItem;
        
        try {
            
            newItem = itemClass.newInstance();
            
            this.selected = newItem;
            
            return newItem;
            
        } catch (InstantiationException ex) {
            
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            
        } catch (IllegalAccessException ex) {
            
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            
        }
        
        return null;
    }

    public void save(ActionEvent event) {
        
        persist(PersistAction.CREATE);
        
    }

    public void edit(ActionEvent event) {
        
        persist(PersistAction.UPDATE);
        
    }

    public void delete(ActionEvent event) {
        
        persist(PersistAction.DELETE);
        
    }

    private void persist(PersistAction persistAction) {

        if (persistAction.equals(PersistAction.CREATE)
                || persistAction.equals(PersistAction.UPDATE)) {

            daoGenerico.saveOrUpdate(selected);

        } else if (persistAction.equals(PersistAction.DELETE)) {

            daoGenerico.delete(selected);

        }
        
        items = null;

    }
    
}
